package gui.components;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class NumericDocument extends PlainDocument {

	private static final long serialVersionUID = 1L;
	private final int min;
	private final int max;

	public NumericDocument(int min, int max) {
		super();
		this.min = min;
		this.max = max;
	}

	@Override
	public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
		int value;
		try {
			value = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			return;
		}
		
		if (value >= min && value <= max) {
			super.insertString(offset, str, attr);
		}
	}
}
